package com.ewanghuiju.app.presenter.my;

import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RxPresenter;
import com.ewanghuiju.app.base.contract.my.RedBeanHomeContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.DataManager;
import com.ewanghuiju.app.model.bean.response.RedBeanDetailsResponBean;
import com.ewanghuiju.app.model.bean.response.RuleBean;
import com.ewanghuiju.app.model.bean.response.WaterDetailsResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.model.http.response.BaseResponse;
import com.ewanghuiju.app.model.http.response.Optional;
import com.ewanghuiju.app.util.RxUtil;
import com.ewanghuiju.app.widget.CommonSubscriber;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Predicate;

/**
 * @author sj  qq:751943509
 * @class describe
 * @time 2020/2/16
 */
public class RedBeanHomePresenter extends RxPresenter<RedBeanHomeContract.View> implements RedBeanHomeContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public RedBeanHomePresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }

    @Override
    public void getData(int page, int type) {
        if (type == 1) {
            addSubscribe(mDataManager.getPtiredBeanInfo(page)
                    .compose(RxUtil.rxSchedulerHelper())
                    .compose(RxUtil.handleMyResultCannull())
                    .subscribeWith(new CommonSubscriber<Optional<RedBeanDetailsResponBean>>(mView) {
                        @Override
                        public void onNext(Optional<RedBeanDetailsResponBean> optional) {
                            if (mView != null)
                                mView.showContent(optional.getIncludeNull());
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            if (mView != null)
                                mView.showContentError();
                        }
                    })
            );
        } else if (type == 2) {
            addSubscribe(mDataManager.getPtiintegralinfo(page)
                    .compose(RxUtil.rxSchedulerHelper())
                    .compose(RxUtil.handleMyResultCannull())
                    .subscribeWith(new CommonSubscriber<Optional<List<RedBeanDetailsResponBean>>>(mView) {
                        @Override
                        public void onNext(Optional<List<RedBeanDetailsResponBean>> optional) {
                            if (mView != null)
                                mView.showWaterContent(optional.getIncludeNull());
                        }

                        @Override
                        public void onError(Throwable e) {
                            super.onError(e);
                            if (mView != null)
                                mView.showContentError();
                        }
                    })
            );
        }
    }

    @Override
    public void ptiredeanProportion(String number) {
        //转化股数
        addSubscribe(mDataManager.ptiredeanProportion(number)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView, true, false) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.ptiredeanProportion(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }

    @Override
    public void increase(String number, String stocks) {
        addSubscribe(mDataManager.ptistocksInfo(number, stocks)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.increaseSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    /**
     * 转增水滴
     *
     * @param number
     * @param mobile
     */
    @Override
    public void ptitransferIntegral(String number, String mobile) {
        addSubscribe(mDataManager.ptitransferIntegral(number, mobile)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<BaseResponse>>(mView) {
                    @Override
                    public void onNext(Optional<BaseResponse> optional) {
                        if (mView != null)
                            mView.increaseSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }

    @Override
    public void getPtistocksInfo() {
        addSubscribe(mDataManager.ptistocksInfo()
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<WaterDetailsResponBean>>(mView) {
                    @Override
                    public void onNext(Optional<WaterDetailsResponBean> optional) {
                        if (mView != null)
                            mView.showWaterDetails(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );

    }

    @Override
    public void getPtiguize(String type) {
        //获取规则
        addSubscribe(mDataManager.getPtiguize(type)
                .compose(RxUtil.rxSchedulerHelper())
                .compose(RxUtil.handleMyResultCannull())
                .subscribeWith(new CommonSubscriber<Optional<RuleBean>>(mView) {
                    @Override
                    public void onNext(Optional<RuleBean> optional) {
                        if (mView != null)
                            mView.showPtiguize(optional.getIncludeNull());
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }
                })
        );
    }


    @Override
    public void attachView(RedBeanHomeContract.View view) {
        super.attachView(view);
        registerEvent();
    }

    /**
     * 刷新列表
     */
    private void registerEvent() {
        addSubscribe(RxBus.getDefault().toFlowable(SendEvent.class)
                .compose(RxUtil.<SendEvent>rxSchedulerHelper())
                .filter(new Predicate<SendEvent>() {
                    @Override
                    public boolean test(@NonNull SendEvent sendEvent) throws Exception {
                        return sendEvent.getType() == Constants.TYPE_GET_WATER_REDBEAN;
                    }
                })
                .subscribeWith(new CommonSubscriber<SendEvent>(mView, "") {
                    @Override
                    public void onNext(SendEvent s) {

                        if (mView != null)
                            mView.refreshData();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        registerEvent();
                    }
                })
        );
    }
}
